﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO.Common;
using ServiceInterfaces.Headquarters;
using System.ServiceModel;
using System.Text;

namespace GUI.Headquarter
{
    public partial class Suppliers : System.Web.UI.Page
    {

        protected List<Supplier> suppliers;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            ChannelFactory<IHeadquartersManagement> factory = new ChannelFactory<IHeadquartersManagement>("headquarter");
            IHeadquartersManagement headquarter = factory.CreateChannel();
            suppliers = headquarter.GetSuppliers();
            factory.Close();
        }

        protected void OnAddSupplier(object sender, EventArgs e)
        {
            if (name.Value.Length == 0 || address.Value.Length == 0)
            {
                this.ShowMessage("Os campos nome e morada são de preenchimento obrigatório", MessageType.Error);
                return;
            }

            try
            {
                Supplier supplier = new Supplier
                {
                    Name = name.Value,
                    Address = address.Value
                };

                ChannelFactory<IHeadquartersManagement> factory = new ChannelFactory<IHeadquartersManagement>("headquarter");
                IHeadquartersManagement headquarter = factory.CreateChannel();
                headquarter.AddSupplier(supplier);
                factory.Close();

                name.Value = string.Empty;
                address.Value = string.Empty;

                this.ShowMessage("Fornecedor adicionado com sucesso", MessageType.MessageDelay);
            }
            catch (Exception excp) {
                this.ShowMessage(string.Format("Ocorreu um erro ao tentar inserir o fornecedor: {0}", excp.Message), MessageType.MessageDelay);
            }
        }
    }
}